package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional // 开启事务
    public void addSetmeal(SetmealDTO setmealDTO) {
        //   保存套餐设计到2张表的操作 -- setmeal 和 setmeal_dish  需要开启事务
        // setmeal
        // 创建setmeal对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 保存setmeal
        setmealMapper.addSetmeal(setmeal);
        // 获取菜品和套餐的关系列表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 设置套餐id
        setmealDishes.forEach(setmealDishe -> {
            setmealDishe.setSetmealId(setmeal.getId());
        });
        // 保存setmeal_dish
        setmealDishMapper.addBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 设置pagehelper
        PageHelper.startPage(
                setmealPageQueryDTO.getPage(),
                setmealPageQueryDTO.getPageSize());
        // 查询信息
        Page<SetmealVO> setmealPage = setmealMapper.list(setmealPageQueryDTO);

        // 返回信息
        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    /**
     * 修改套餐状态
     *
     * @param status
     * @param id
     */
    @Override
    @Transactional //开启事务
    public void changeStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getMsgById(Long id) {
        //  先查询setmeal的数据
        Setmeal setmeal = setmealMapper.getMsgById(id);
        //  再查询setmeal_dish的数据
        List<SetmealDish> setmealDishList = setmealDishMapper.getMsgBySetmealId(id);
        //  合并最后返回
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional //开启事务
    public void update(SetmealDTO setmealDTO) {
        //     更新setmeal中的数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        //     删除setmeal_dish中与setmeal绑定的数据
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //     添加setmeal_dish中的数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.addBatch(setmealDishes);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional //开启事务
    public void deletes(List<Long> ids) {
        //     判断套餐是否起售
        ids.forEach(id -> {
            Setmeal setmeal =setmealMapper.getMsgById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //     删除setmeal数据
        setmealMapper.deleteBatch(ids);
        //     删除setmeal_dish数据
        ids.forEach(id->{
            setmealDishMapper.deleteBySetmealId(id);
        });
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getMsgsByCategoryId(Long categoryId) {
        // 补充数据
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);
        List<Setmeal> setmealList = setmealMapper.getMsgsByCategoryId(setmeal);
        return setmealList;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishesItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
